import { getDetails } from "@/admin/api/user";
import { validatePass } from "~/utils/validate";
var validateUsername = (rule, value, callback) => {
	getDetails(value).then(response => {
		if (window.boxType === "edit") callback();
		let result = response.data.data;
		if (result !== null) {
			callback(new Error("用户名已经存在"));
		} else {
			callback();
		}
	});
};

// 设置手机号的验证规则
const checkPhone = (rule, value, callback) => {
	if (!value) {
		callback(new Error("请输入联系方式"));
	} else {
		const reg = /^1[3|4|5|7|8][0-9](\d|\*){4}\d{4}$/;
		if (reg.test(value)) {
			callback();
		} else {
			return callback(new Error("请输入正确的电话"));
		}
	}
};

export const tableOption = {
	border: true,
	index: true,
	indexLabel: "序号",
	stripe: true,
	menuAlign: "center",
	searchMenuSpan: 24,
	editBtn: false,
	delBtn: false,
	align: "center",
	addBtn: false,
	dialogClickModal: false,
	column: [
		{
			fixed: true,
			label: "id",
			prop: "userId",
			span: 24,
			hide: true,
			editDisabled: true,
			addDisplay: false,
			editDisplay: false
		},
		{
			fixed: true,
			label: "真实姓名",
			prop: "realName",
			search: true,
			span: 24,
			rules: [
				{
					required: true,
					message: "请输入真实姓名"
				},
				{
					min: 2,
					max: 20,
					message: "长度在 2 到 20 个字符",
					trigger: "blur"
				}
			]
		},
		{
			fixed: true,
			label: "身份证",
			prop: "idCard",
			type: "input",
			value: "",
			//editDisabled: true,
			//addDisplay: true,
			span: 24,
			rules: [
				{
					min: 18,
					max: 18,
					message: "身份证必须是18位",
					trigger: "blur"
				}
			]
		},
		{
			fixed: true,
			label: "用户名",
			prop: "username",
			editDisabled: true,
			slot: true,
			search: true,
			span: 24,
			rules: [
				{
					required: true,
					message: "请输入用户名"
				},
				{
					min: 3,
					max: 20,
					message: "长度在 3 到 20 个字符",
					trigger: "blur"
				},
				{ validator: validateUsername, trigger: "blur" }
			]
		},
		{
			fixed: true,
			label: "密码",
			prop: "password",
			type: "password",
			value: "",
			hide: true,
			editDisplay: false,
			span: 24,
			rules: [
				{
					validator: validatePass,
					trigger: "blur"
				}
			]
		},
		{
			fixed: true,
			label: "照片上传",
			prop: "avatars",
			type: "upload",
			listType: "picture-img",
			span: 24,
			hide: true,
			loadText: "照片上传中，请稍等",
			propsHttp: {
				res: "data"
			},
			tip: "只能上传jpg/png文件，且不超过10M",
			action: "/admin/sys-file/upload",
			formslot: true,
			limit: "1",
			slot: true
		},
		{
			fixed: true,
			label: "手机号",
			prop: "phone",
			type: "input",
			value: "",
			span: 24,
			rules: [
				{
					required: true,
					message: "请输入手机号"
				},
				{ validator: checkPhone, trigger: "blur" }
			]
		},
		{
			fixed: true,
			label: "所属部门",
			prop: "deptId",
			formslot: true,
			//search: true,
			slot: true,
			span: 24,
			hide: true,

			rules: [
				{
					required: true,
					message: "请选择部门",
					trigger: "change"
				}
			]
		},

		{
			fixed: true,
			label: "部门",
			prop: "deptName",

			overHidden: true,
			addDisplay: false,
			editDisplay: false,
			span: 24
		},
		{
			fixed: true,
			label: "角色",
			prop: "role",
			formslot: true,
			slot: true,
			overHidden: true,
			span: 24,
			rules: [
				{
					required: true,
					message: "请选择角色",
					trigger: "blur"
				}
			]
		},
		{
			fixed: true,
			label: "状态",
			prop: "lockFlag",
			type: "radio",
			slot: true,
			border: true,
			span: 24,
			//search: true,
			rules: [
				{
					required: true,
					message: "请选择状态",
					trigger: "blur"
				}
			],
			dicData: [
				{
					label: "有效",
					value: "0"
				},
				{
					label: "无效",
					value: "2"
				},
				{
					label: "锁定",
					value: "9"
				}
			]
		},
		{
			fixed: true,
			width: 120,
			label: "创建时间",
			prop: "createTime",
			type: "input",
			editDisabled: false,
			addDisplay: false,
			disabled: true,
			span: 24
		},
		{
			label: "头像路径",
			prop: "avatar",
			display: false,
			hide: true
		}
	]
};
